package com.yzhn.demo.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.ypay.apppay.ServiceFactory;
import com.yzhn.demo.R;


public class WXPayEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wechat_pay_result);
        ServiceFactory.getWXAPI().handleIntent(getIntent(), this);
    }
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        ServiceFactory.getWXAPI().handleIntent(intent, this);
    }
    // 微信发送请求到第三方应用时，会回调到该方法
    @Override
    public void onReq(BaseReq req) {
        Log.e("onReq", "onReq");
    }
    // 第三方应用发送到微信的请求处理后的响应结果，会回调到该方法
    @Override
    public void onResp(BaseResp resp) {
        Log.d("zyf", "onPayFinish,errCode=" + resp.errCode);
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {//微信支付回调
            switch (resp.errCode) {
                case BaseResp.ErrCode.ERR_OK:// 支付成功
                    Log.e("WXPayEntryActivity", "ERR_OK");
                    Toast.makeText(this, "支付成功", Toast.LENGTH_SHORT).show();
                    break;
                case BaseResp.ErrCode.ERR_COMM:// 错误
                    Log.e("WXPayEntryActivity", "ERR_COMM");
                    Toast.makeText(this, "错误", Toast.LENGTH_SHORT).show();
                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL:// 用户取消
                    Log.e("WXPayEntryActivity", "ERR_USER_CANCEL");
                    Toast.makeText(this, "用户取消", Toast.LENGTH_SHORT).show();

                    break;
                default:
                    break;
            }
        }
        finish();
    }

}